﻿//using Newtonsoft.Json;
//using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wubi86
{
    public delegate void SetNextFocuse(int idx);
    public delegate void CountWord(int cnt, int inputCnt);
    public partial class UserControl1 : UserControl
    {
        public SetNextFocuse SetNextFocuse;
        public CountWord CountWord;
        public UserControl1(string txt) {
            InitializeComponent();
            label1.Text = txt;
           // byte[] wubi86 = Properties.Resources.wubi86;
           // string wubi86json = System.Text.UTF8Encoding.UTF8.GetString(wubi86);
            // JObject o = JsonConvert.DeserializeObject(wubi86json) as JObject;
            Dictionary<string, string> o = Utils.Wubi86KeyValues;
            //  JToken v;
            string v;
            if (o.TryGetValue(txt, out v)) {
                string wubi86code = v;
                if (wubi86code.Contains(",")) {
                    wubi86code = wubi86code.Split(',')[0];
                    toolTip1.SetToolTip(label2, "【" +txt + "】有多个五笔编码：" +v);
                    label2.ForeColor = Color.Brown;
                }
                label2.Text = wubi86code;                
            } else {
                label2.Text = txt;
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e) {
            if (textBox3.Text != "") {
                SetNextFocuse(int.Parse(this.Tag.ToString()) + 1);
                FrmMain.inputCnt++;
                if (label1.Text != textBox3.Text) {
                    label1.ForeColor = Color.Red;
                    FrmMain.errCnt++;
                } else {
                    if (label1.ForeColor == Color.Red) {
                        if (FrmMain.errCnt > 0)
                            FrmMain.errCnt--;
                    }
                    label1.ForeColor = Color.Blue;
                }
            } else {
                if (label1.ForeColor == Color.Red) {
                    if (FrmMain.errCnt > 0)
                        FrmMain.errCnt--;
                }
                label1.ForeColor = Color.Black;          
            }
            int cntword = 0;
            if (FrmMain.startTime == DateTime.MinValue)
                FrmMain.startTime = DateTime.Now;
            else {
                double d = (DateTime.Now - FrmMain.startTime).TotalSeconds;
                cntword = (int)(((double)FrmMain.inputCnt / d) * 60);
            }
            CountWord(FrmMain.errCnt, cntword);
        }

        private void textBox3_KeyUp(object sender, KeyEventArgs e) {

        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Back) {
                if (textBox3.Text == "") {
                    SetNextFocuse(int.Parse(this.Tag.ToString()) - 1);
                }
            } else if (e.KeyCode == Keys.Enter) {
                SetNextFocuse(int.Parse(this.Tag.ToString()) + 1);
            }
        }
    }
}
